<template>
	<view>
		<web-view :src="outlink" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	import util from './../../utils/util.js';
	const app = getApp();
	export default {
		data() {
			return {
				outlink: null,
				type: null,
				title:null,
				imageUrl:null,
				shareLink:null
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		methods: {
			/**
			 * 递归解码
			 */
			decode(url) {
				let u = util.decodeUrl(url)
				if (u.indexOf('/') === -1) {
					return this.decode(u)
				} else {
					return u
				}
			},
			handleMessage(options) {
				console.log(options,'触发更新')
				let data = options.detail.data
				this.title = data[data.length-1].title;
				this.shareLink =  data[data.length-1].path;
				this.imageUrl = data[data.length-1].imageUrl
			},
		},

		onLoad: function(options) {
			console.log('outlink页面参数-', options)
			let outlink = this.decode(options.outlink);
			uni.setStorageSync('outlink',outlink)
			if(outlink.indexOf('stopAuth') != -1 && outlink.indexOf('previewer') != -1){
				let openid = uni.getStorageSync('openid');
				outlink = outlink + '&openId=' + openid;
			}
			//泰惠玩H5页面
			let MiniOpenId = uni.getStorageSync('MiniOpenId');
			if(outlink.indexOf('h5no1') != -1 || outlink.indexOf('tuzhan') != -1 || outlink.indexOf('tuzhanyun') != -1) { 
				let linkArr = outlink.split('#')
				if(!MiniOpenId) {
					uni.switchTab({
						url: '/pages/my/my'
					})
				} else if (outlink.indexOf('inviteUser') != -1) {  //拼团分享进来，带参数
					let linkArr = outlink.split('#')
				    outlink = linkArr[0] + '&' + 'MiniOpenId=' + MiniOpenId + '#' + linkArr[1];
				} else if (outlink.indexOf('groupBuy') != -1) {  //首页拼团直接跳转拼团页
				    outlink = linkArr[0] + '?' + 'MiniOpenId=' + MiniOpenId + '#' + linkArr[1];
				} else {
					if(outlink.indexOf('&') != -1) {
						//分享非参团页面时有其他参数
						outlink = outlink + '&MiniOpenId=' + MiniOpenId + '#/';
					} else {   
						//首页泰惠玩直接进来
						outlink = outlink + '?MiniOpenId=' + MiniOpenId + '#/';
					}
				}
			}
			console.log('outlink:', outlink);
			this.outlink = outlink
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		// onShow: function() {
		// 	this.onLoad()
		// },
		onShareAppMessage: function(options) {
			console.log(
				"==============================index onshow====================================================")
			console.log('分享链接==', this.shareLink)
			console.log('分享标题==',this.title)
			console.log('分享图片地址==',this.imageUrl)
			let url = this.shareLink ? encodeURIComponent(this.shareLink) : this.outlink
			return {
				title:this.title,
				imageUrl:this.imageUrl,
				path:`/pages/outlink/outlink?outlink=${url}` 
			}
		},
	};
</script>
<style>
</style>
